$(function () {
    obj.init();
});

var obj = new function() {
    return  {
        params: {
            userId: null
        },
        dom: {
            send: null,
            phone: null,
            code: null,
            grade: null,
            term: null,
            submit: null
        },
        countdown: 60,
        init: function () {
            this.params.userId = xhTool.getQuery('userId');

            this.dom.send  = $('#bt_send');
            this.dom.phone = $('#phone_num');
            this.dom.code  = $('#valid_num');
            this.dom.grade = $('input[name=grade]');
            this.dom.term  = $('input[name=term]');
            this.dom.submit= $('#bt_submit');

            this.listener();
        },
        listener: function () {
            var $this = this;
            //发送验证码
            $this.dom.send.click(function () {
                $this.funObj().sendCode();
            });
            //提交表单
            $this.dom.submit.click(function () {
                $this.funObj().submitForm()
            });
        },
        funObj: function () {
            var $this = this;
            return {
                //发送验证码
                sendCode: function () {
                    var phoneNum = $this.dom.phone.val();
                    if (!phoneNum) {
                        $this.funObj().showError('手机号不能为空！');
                        return false;
                    }
                    if (phoneNum.length !== 11) {
                        $this.funObj().showError('手机号位数不正确！');
                        return false;
                    }
                    // try {
                    //     xhExtra.ajaxRequest('survey/welfare/getValidCode', {phoneNum: phoneNum}, {
                    //         success: true,
                    //         check: true
                    //     });
                    // }
                    // catch (e) {
                        $.ajax({
                            url: baseUrl + 'survey/welfare/getValidCode',
                            type: 'POST',
                            data: {
                                phoneNum: phoneNum
                            },
                            dataType: 'json',
                            complete: function(XMLHttpRequest){
                                var result = XMLHttpRequest.responseJSON || XMLHttpRequest.responseText;
                                if(result.error){
                                    alert(result.error);
                                }
                                else {
                                    alert(result.msg);
                                }
                            }
                        });
                    // }
                    $this.funObj().setCodeTime();
                },
                //发送验证码倒计时
                setCodeTime: function () {
                    if ($this.countdown === 0) {
                        $this.dom.send.attr('disabled', false);
                        $this.dom.send.text('获取验证码');
                        $this.countdown = 60;
                        return;
                    } else {
                        $this.dom.send.attr('disabled', true);
                        $this.dom.send.text("重新发送(" + $this.countdown + ")");
                        $this.countdown--;
                    }
                    setTimeout(function() {
                        $this.funObj().setCodeTime();
                    }, 1000)
                },
                submitForm: function () {
                    var params = {
                        userId: $this.params.userId,
                        phoneNum: $this.dom.phone.val(),
                        validCode: $this.dom.code.val(),
                        grade: $this.dom.grade.filter(':checked').val(),
                        term: $this.dom.term.filter(':checked').val()
                    };
                    if (!params.phoneNum) {
                        $this.funObj().showError('手机号不能为空！');
                        return false;
                    }
                    if (params.phoneNum.length !== 11) {
                        $this.funObj().showError('手机号位数不正确！');
                        return false;
                    }
                    if (!params.validCode) {
                        $this.funObj().showError('验证码不能为空！');
                        return false;
                    }
                    if (params.validCode.length !== 6) {
                        $this.funObj().showError('验证码位数不正确！');
                        return false;
                    }
                    if (!params.grade) {
                        $this.funObj().showError('请选择年级！');
                        return false;
                    }
                    if (!params.term) {
                        $this.funObj().showError('请选择上课期次！');
                        return false;
                    }
                    // try {
                    //     xhExtra.askChangeStatus('survey/welfare/saveKokugaku', params, null, '确认提交？', function () {
                    //         window.location.href = baseUrl + 'survey/welfare/thank';
                    //     });
                    // }
                    // catch (e) {
                        if (confirm('确认提交？')) {
                            $.ajax({
                                url: baseUrl + 'survey/welfare/saveKokugaku',
                                type: 'POST',
                                data: params,
                                dataType: 'json',
                                complete: function(XMLHttpRequest){
                                    var result = XMLHttpRequest.responseJSON || XMLHttpRequest.responseText;
                                    if(result.error){
                                        alert(result.error);
                                        return true;
                                    }
                                    window.location.href = baseUrl + 'survey/welfare/thank';
                                }
                            });
                        }
                    // }
                },
                showError: function (msg) {
                    // try {
                    //     xhTop.showError(msg);
                    // }
                    // catch (e) {
                        alert(msg);
                    // }
                }
            }
        }
    }
};